﻿
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
using BCEntity.TMS.TransportSpecificServiceConfig;

namespace BCData.TMS.TransportSpecificServiceConfig
{
    public class TransportSpecificServiceConfigData : ITransportSpecificServiceConfigData
    {
        private readonly IDatabaseContext database;
        private readonly string QUERY = @"SELECT
                                                                A.TransportSpecificServiceConfigId, 
                                                                A.VehicleTypeId,
                                                                A.VehicleTypeName,
                                                                A.SpecificServiceType, 
                                                                D.ServiceTypeName,
                                                                A.ServiceTitle, 
                                                                A.ShortName,
                                                                A.HasRemark, 
                                                                A.ServiceRemark, 
                                                                A.ServiceAmount, 
                                                                A.HasUnit, 
                                                                A.ServiceUnit, 
                                                                A.CreateAdminId, 
                                                                A.CreateAdminName, 
                                                                A.CreateTime, 
                                                                A.UpdateTime, 
                                                                A.EditAdminId, 
                                                                A.EditAdminName
                                                                FROM
                                                                transportspecificserviceconfig AS A
                                                                INNER JOIN
                                                                specificservicetype AS D
                                                                ON 
                                                                A.SpecificServiceType = D.SpecificServiceType ";



        #region 构造函数
        public TransportSpecificServiceConfigData(IDatabaseContext databaseContext)
        {
            this.database = databaseContext;
        }
        #endregion

        #region InsertAsync
        /// <summary>
        /// 添加新运输特殊服务费配置纪录
        /// </summary>
        /// <param name="transportSpecificServiceConfigEntity"></param>
        /// <returns></returns>
        public async Task<TransportSpecificServiceConfigEntity> InsertAsync(TransportSpecificServiceConfigEntity entity)
        {
            return await this.database.Master.InsertAsync(entity);
        }
        #endregion

        #region updateAsync
        /// <summary>
        /// 修改运输特殊服务费配置记录
        /// </summary>
        /// <param name="transportSpecificServiceConfigEntity"></param>
        /// <returns></returns>
        public async Task<TransportSpecificServiceConfigEntity> UpdateAsync(TransportSpecificServiceConfigEntity entity)
        {
            return await this.database.Master.UpdateAsync(entity);
        }
        #endregion 

        #region Delete
        /// <summary>
        /// 删除运输特殊服务费配置记录
        /// </summary>
        /// <param name="transportSpecificServiceConfigId"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(long transportSpecificServiceConfigId)
        {
            string sql = "DELETE FROM transportspecificserviceconfig WHERE TransportSpecificServiceConfigId = ?transportSpecificServiceConfigId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?TransportSpecificServiceConfigId", transportSpecificServiceConfigId, DbType.Int64);
            return await this.database.Master.ExecuteAsync(sql, parameters, database.Transaction) > 0;
        }
        #endregion

        #region GetById
        /// <summary>
        /// 获取运输特殊服务费配置记录
        /// </summary>
        /// <param name="transportSpecificServiceConfigId"></param>
        /// <returns></returns>
        public async Task<TransportSpecificServiceConfigEntity> GetEntityAsync(long transportSpecificServiceConfigId)
        {
            string sql = QUERY + " WHERE  TransportSpecificServiceConfigId = ?transportSpecificServiceConfigId";

            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?TransportSpecificServiceConfigId", transportSpecificServiceConfigId, DbType.Int64);
            return await this.database.Master.QueryFirstOrDefaultAsync<TransportSpecificServiceConfigEntity>(sql, parameters);
        }
        #endregion

        #region GetListAsync 查询运输特殊服务费配置记录
        public async Task<(IEnumerable<TransportSpecificServiceConfigEntity>, int)> GetListAsync(int? vehicleTypeId,string specificServiceType, string serviceTitle, int pageIndex, int pageSize)
        {

            DynamicParameters parameters = new DynamicParameters();
            //查询条件
            List<string> wheres = new List<string>();

            if (vehicleTypeId.HasValue)
            {
                wheres.Add("A.VehicleTypeId=?VehicleTypeId");
                parameters.Add("?VehicleTypeId", vehicleTypeId.Value, DbType.Int64);
            }
            if (!string.IsNullOrWhiteSpace(specificServiceType))
            {
                wheres.Add("A.SpecificServiceType LIKE ?SpecificServiceType");
                parameters.Add("?SpecificServiceType", $"%{specificServiceType}%", DbType.AnsiString);
            }
            if (!string.IsNullOrWhiteSpace(serviceTitle))
            {
                wheres.Add("A.ServiceTitle LIKE ?ServiceTitle");
                parameters.Add("?ServiceTitle", $"%{serviceTitle}%", DbType.AnsiString);
            }


            string sql = string.Format(@"{0} {1} LIMIT {2},{3} ; SELECT COUNT(1) FROM  transportspecificserviceconfig A {1}",
                   QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
                  pageSize
                  );

            return await this.database.QueryMultipleAsync<TransportSpecificServiceConfigEntity, int>(sql, parameters);
        }
        #endregion

        #region GetListAsync 查询运输特殊服务费配置记录
        public async Task<IEnumerable<TransportSpecificServiceConfigEntity>> GetOnlyListAsync(int? vehicleTypeId, string specificServiceType, string serviceTitle)
        {

            DynamicParameters parameters = new DynamicParameters();
            //查询条件
            List<string> wheres = new List<string>();

            if(vehicleTypeId.HasValue)
            {
                wheres.Add("A.VehicleTypeId=?VehicleTypeId");
                parameters.Add("?VehicleTypeId", vehicleTypeId.Value, DbType.Int64);
            }
            if (!string.IsNullOrWhiteSpace(specificServiceType))
            {
                wheres.Add("A.SpecificServiceType LIKE ?SpecificServiceType");
                parameters.Add("?SpecificServiceType", $"%{specificServiceType}%", DbType.AnsiString);
            }
            if (!string.IsNullOrWhiteSpace(serviceTitle))
            {
                wheres.Add("A.ServiceTitle LIKE ?ServiceTitle");
                parameters.Add("?ServiceTitle", $"%{serviceTitle}%", DbType.AnsiString);
            }

            string sql = string.Format(@"{0} {1} ",
                   QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : ""
                  );

            return await this.database.Slave.QueryAsync<TransportSpecificServiceConfigEntity>(sql, parameters);
        }
        #endregion

    }
}
//接口



